package org.ruoyi.data.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.ruoyi.common.core.domain.R;
import org.ruoyi.common.core.validate.AddGroup;
import org.ruoyi.common.core.validate.EditGroup;
import org.ruoyi.common.excel.utils.ExcelUtil;
import org.ruoyi.common.idempotent.annotation.RepeatSubmit;
import org.ruoyi.common.log.annotation.Log;
import org.ruoyi.common.log.enums.BusinessType;
import org.ruoyi.common.mybatis.core.page.PageQuery;
import org.ruoyi.common.mybatis.core.page.TableDataInfo;
import org.ruoyi.common.web.core.BaseController;
import org.ruoyi.data.domain.bo.DataTableBo;
import org.ruoyi.data.domain.vo.DataTableVo;
import org.ruoyi.data.service.IDataTableService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;


/**
 * 数据
 *
 * @author bz_huaYal9146
 * @date 2025-11-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/data/table")
public class DataTableController extends BaseController {

    private final IDataTableService dataTableService;

    /**
     * 查询数据列表
     */
    @SaCheckPermission("data:table:list")
    @GetMapping("/list")
    public TableDataInfo<DataTableVo> list(DataTableBo bo, PageQuery pageQuery) {
        return dataTableService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出数据列表
     */
    @SaCheckPermission("data:table:export")
    @Log(title = "数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(DataTableBo bo, HttpServletResponse response) {
        List<DataTableVo> list = dataTableService.queryList(bo);
        ExcelUtil.exportExcel(list, "数据", DataTableVo.class, response);
    }

    /**
     * 获取数据详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("data:table:query")
    @GetMapping("/{id}")
    public R<DataTableVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(dataTableService.queryById(id));
    }

    /**
     * 新增数据
     */
    @SaCheckPermission("data:table:add")
    @Log(title = "数据", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody DataTableBo bo) {
        return toAjax(dataTableService.insertByBo(bo));
    }

    /**
     * 修改数据
     */
    @SaCheckPermission("data:table:edit")
    @Log(title = "数据", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody DataTableBo bo) {
        return toAjax(dataTableService.updateByBo(bo));
    }

    /**
     * 删除数据
     *
     * @param ids 主键串
     */
    @SaCheckPermission("data:table:remove")
    @Log(title = "数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return dataTableService.deleteWithValidByIds(List.of(ids), true);
    }
}
